/*
 * Copyright 2008 Robert Hanson <iamroberthanson AT gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.penlets.core;

import com.livescribe.display.Display;
import com.livescribe.event.PenTipListener;
import com.livescribe.event.StrokeListener;
import com.livescribe.penlet.Penlet;
import com.livescribe.penlet.PenletStateChangeException;
import com.livescribe.ui.ScrollLabel;

/**
 * Abstract penlet class providing basic default functionality.
 * Uses a ScrollLabel for displaying text messages.
 * @author robert.hanson
 */
public class AbstractPenlet extends Penlet
{
    private Display display;
    private ScrollLabel label;
    
    

    /**
     * Final method, override onInit() to receive initialization calls.
     */
    public final void initApp () throws PenletStateChangeException
    {
        display = getContext().getDisplay();
        label = new ScrollLabel();
        try {
            onInit();
        }
        catch (Exception e) {
            throw new PenletStateChangeException(e.getMessage());
        }
    }

    /**
     * Method can be overridden if you need to handle the
     * initialize event.
     */
    protected void onInit () throws Exception
    {
    }
    
    /**
     * Final method, override onActivate() to receive activation calls.
     */
    public final void activateApp (int reason, Object[] args)
    {
        if (reason == Penlet.ACTIVATED_BY_MENU) {
            display.setCurrent(label);
            onMenuActivation(args);
        }
        onActivation(args);
    }

    /**
     * Method can be overridden if you need to handle the
     * penlet activation event where the reason is menu-activation.
     */
    protected void onMenuActivation (Object[] args)
    {
    }

    /**
     * Method can be overridden if you need to handle the
     * penlet app activation event.
     */
    protected void onActivation (Object[] args)
    {
    }

    /**
     * Final method, override onDeactivate() to receive deactivation calls.
     */
    public final void deactivateApp (int reason)
    {
        onDeactivation();
    }

    /**
     * Method can be overridden if you need to handle the
     * penlet app deactivation event.
     */
    protected void onDeactivation ()
    {
    }

    /**
     * Final method, override onDestroy() to receive destroy calls.
     */
    public final void destroyApp () throws PenletStateChangeException
    {
        onDestroy();
    }

    
    /**
     * Called when the penlet is unloaded or when
     * the VM is shut down.
     */
    private void onDestroy ()
    {
        // TODO Auto-generated method stub
        
    }

    /**
     * Sets the message displayed in the OLED display
     * with scrolling of the message enabled.
     */
    protected void setMessage (String message)
    {
        label.draw(message, true);
    }

    
    /**
     * Adds a PenTipListener to the penlet context.
     * @param listener
     */
    protected void addPenTipListener (PenTipListener listener)
    {
        getContext().addPenTipListener(listener);
    }

    /**
     * Adds a StrokeListener to the penlet context.
     * @param listener
     */
    protected void addStrokeListener (StrokeListener listener)
    {
        getContext().addStrokeListener(listener);
    }

}
